package com.cy.pj.brand.controller;

import com.cy.pj.brand.pojo.Brand;
import com.cy.pj.brand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class BrandController {
    @Autowired
    private BrandService brandService;
    //rest风格(一种软件架构编码风格)的url定义
    //其语法为：a/b/{c}/d/{e},在URL可以定义变量，这个变量需要使用{}括起来
    //rest风格可以更好实现跨平台
    //@PathVariable 注解用于修饰方法参数，目的是告诉spring mvc,参数的值来url
   //查询业务处理
    //方式二-->brand.html 基于js提交方式
    @RequestMapping(value = {"/brand/doFindBrands","/brand/doFindBrands/{name}"})
    //方式一  传统url方式的定义
   // @RequestMapping(value = {"/brand/doFindBrands"})
    public String doFindBrands(@PathVariable(required = false) String name, Model model){
        List<Brand> list=brandService.findBrands(name);
        model.addAttribute("list",list);
        return "brand/brand";//第一个brand为目录；第二个brand为view name
    }

    //删除业务处理
    @RequestMapping("/brand/doDeleteBrands/{id}")
    public String doDeleteBrands(@PathVariable Integer id,Model model){
        brandService.deleteById(id);
        List<Brand> list=brandService.findBrands(null);
        model.addAttribute("list",list);
        return "brand/brand";
        //方式二 删除后跳转回查询方法
       // return "redirect:/brand/doFindBrands";
    }
    //添加业务处理
    @RequestMapping("/brand/doInsetBrands")
    public String doInsetBrands(Brand entity){
        brandService.insertBrand(entity);
        return "redirect:/brand/doFindBrands";
    }
    @RequestMapping("/brand/doBrandAddUI")
    public String doBrandAddUI() {
        return "/brand/addbrand";
    }

    //修改业务处理
    @RequestMapping("/brand/doUpdateById/{id}")
    public String doUpdateById(@PathVariable Integer id,Model model){
        Brand brand=brandService.findByIds(id);
        model.addAttribute("brand", brand);
        return "/brand/updatebrand";
    }

    @RequestMapping("/brand/doUpdateBrands")
    public String doUpdateBrands( Brand brand){
        brandService.updateBrand(brand);
        return "redirect:/brand/doFindBrands";
    }
}
